import { PlanGetPage, GetScoreDetail } from "@/api/teacher/plan";
import { GetStuByClass } from "@/api/admin/teacher";
import { ClassGetAll } from "@/api/teacher/zsclass";
import {
  GetmoduleList,
  AnswerQuery,
  type Answerlist
} from "@/api/teacher/answer";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted, watch } from "vue";
import qs from "qs";
import { ElMessage, FormInstance } from "element-plus";
export function useExample() {
  const form = reactive({
    planId: "",
    classId: "",
    userId: "",
    submodulId: ""
  });
  const refform = ref();
  const planList = ref<any>([]);
  const classList = ref<any>([]);
  const userList = ref<any>([]);
  const submoduList = ref([]);
  const datalist = ref<Answerlist>();
  const classId = ref();
  const loading = ref(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const rules = {
    planId: [
      {
        required: true,
        message: "请选择计划名称",
        trigger: ["blur", "change"]
      }
    ],
    classId: [
      {
        required: true,
        message: "请选择班级名称",
        trigger: ["blur", "change"]
      }
    ],
    userId: [
      {
        required: true,
        message: "请选择用户名称",
        trigger: ["blur", "change"]
      }
    ],
    submodulId: [
      {
        required: true,
        message: "请选择模块名称",
        trigger: ["blur", "change"]
      }
    ]
  };
  const sumup = ref<string>();
  // 选择班级名称：
  async function HandClassId(row) {
    form.userId = ""
    if (!row) {
      userList.value = []
      return
    }
    const data = qs.stringify({ classId: row });
    const res = await GetStuByClass(data);
    if (res.success) {
      userList.value = res.data;
    } else {
      ElMessage.error(res.msg)
    }
  }

  async function onSearch(row: FormInstance | undefined) {
    (row as any).validate(async valid => {
      if (!valid) return;
      let params = qs.stringify({
        planId: form.planId,
        classId: form.classId,
        userId: form.userId,
        type: form.submodulId
      })
      const res = await GetScoreDetail(params);
      if (res.success) {
        sumup.value = res.data;
      } else {
        ElMessage.error(res.msg)
      }
    })
  }
  //初始化下拉框数据
  async function initSelect() {
    //获取班级下拉框
    const response = await ClassGetAll();
    if (response.success) classList.value = response.data;
    else ElMessage.error('获取班级失败')
    //获取计划下拉框
    const { data, success } = await PlanGetPage({
      pageindex: 1,
      pagesize: 100,
      PlanName: ""
    });
    if (success) planList.value = data?.items;
    else ElMessage.error('获取计划失败')
    //获取模块下拉框
    const SelectType = qs.stringify({
      SelectType: "MKMC"
    });
    const res = await GetmoduleList(SelectType);
    if (res.success) submoduList.value = res.data;
    else ElMessage.error('获取模块失败');
  }

  function resetForm(row) {
    sumup.value = ""
    row.resetFields()
    // for (let key in form) {
    //   form[key] = undefined
    // }
    // sumup.value = ''
  };

  onMounted(() => {
    initSelect();
  });
  return {
    form,
    rules,
    refform,
    loading,
    classId,
    userList,
    datalist,
    submoduList,
    onSearch,
    resetForm,
    HandClassId,
    classList,
    planList,
    sumup,
  };
}
